package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.query.DestinationQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Collections;

/**
 * @author 猫教授
 * @date 2019/8/9 15:36
 */
@Controller
@RequestMapping("destination")
public class DestinationController {
    
    @Reference
    private IDestinationService destinationService;


    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") DestinationQuery qo, Model model) {
      PageInfo<Destination> pageInfo=destinationService.listAll(qo);
      model.addAttribute("pageInfo",pageInfo);
      //吐丝使用迭代从低级到高级
        ArrayList<Destination> toasts=new ArrayList<>();
        //如果为-1证明是根一级的所以就是输出空集合就行
        if(qo.getParentId()==-1L){
            model.addAttribute("toasts",toasts);
        }else {
            getToasts(qo.getParentId(),toasts);
            //并且还要反转
            Collections.reverse(toasts);
            model.addAttribute("toasts",toasts);
        }
      return "destination/list";
    }

    /**
     * 内部方法用来迭代吐丝的
     * @param destinationId
     * @param toasts
     */
    private void getToasts(Long destinationId,ArrayList toasts){
        Destination destination = destinationService.selecOne(destinationId);
        toasts.add(destination);
        if (destination.getParent()!=null){
            getToasts(destination.getParent().getId(),toasts);
        }
    }







}
